﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Microwave02: Microwave.cpp Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderCpp
            </h1>
            <h3>
                Microwave02: Microwave.cpp Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">Microwave.cpp</div>  </div>
</div>
<div class="contents">
<a href="_microwave_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include &lt;stdexcept&gt;</span>
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;logging.hpp&gt;</span>
<a name="l00003"></a>00003 <span class="preprocessor">#include &lt;SmBoostObserver.h&gt;</span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &quot;<a class="code" href="_microwave_8h.html">Microwave.h</a>&quot;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &quot;Microwave02Fsm.h&quot;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00007"></a>00007 
<a name="l00008"></a>00008 <span class="keyword">using namespace </span>std;
<a name="l00009"></a>00009 
<a name="l00010"></a><a class="code" href="class_microwave.html#a4841456633df59a69ae8f4cc07e37362">00010</a> <a class="code" href="class_microwave.html#a4841456633df59a69ae8f4cc07e37362">Microwave::Microwave</a>() :
<a name="l00011"></a>00011 m_bRun(true),
<a name="l00012"></a>00012 m_eventsCountDoor(0),
<a name="l00013"></a>00013 m_eventsCountUser(0)
<a name="l00014"></a>00014 {
<a name="l00015"></a>00015     <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a> = <span class="keyword">new</span> MicrowaveContext(*<span class="keyword">this</span>);
<a name="l00016"></a>00016     <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;SetObserver(SmBoostObserver::GetInstance());
<a name="l00017"></a>00017     <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;EnterInitialState();
<a name="l00018"></a>00018 }
<a name="l00019"></a>00019 
<a name="l00020"></a><a class="code" href="class_microwave.html#a6363c148c2f2c15047505d90f271497a">00020</a> <a class="code" href="class_microwave.html#a6363c148c2f2c15047505d90f271497a">Microwave::~Microwave</a>()
<a name="l00021"></a>00021 {
<a name="l00022"></a>00022     <span class="keyword">delete</span> <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>;
<a name="l00023"></a>00023 }
<a name="l00024"></a>00024 
<a name="l00025"></a><a class="code" href="class_microwave.html#a9de439ae4e3768a815cd8653697123dc">00025</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a9de439ae4e3768a815cd8653697123dc">Microwave::doorWorker</a>()
<a name="l00026"></a>00026 {
<a name="l00027"></a>00027     <span class="keywordflow">while</span>(<a class="code" href="class_microwave.html#aab9533a3e37add11113ab8333a99e17a">m_bRun</a> == <span class="keyword">true</span>){
<a name="l00028"></a>00028         <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;evDoorOpened();
<a name="l00029"></a>00029         <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;evDoorClosed();
<a name="l00030"></a>00030         <a class="code" href="class_microwave.html#ac15ca147a5c1c50b84bfc381d49e30b8">m_eventsCountDoor</a> += 2;
<a name="l00031"></a>00031     }
<a name="l00032"></a>00032 }
<a name="l00033"></a>00033 
<a name="l00034"></a><a class="code" href="class_microwave.html#ad4dbfdafd37a85475b02e5e1a50c6b22">00034</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#ad4dbfdafd37a85475b02e5e1a50c6b22">Microwave::userWorker</a>()
<a name="l00035"></a>00035 {
<a name="l00036"></a>00036     <span class="keywordflow">while</span>(<a class="code" href="class_microwave.html#aab9533a3e37add11113ab8333a99e17a">m_bRun</a> == <span class="keyword">true</span>){
<a name="l00037"></a>00037         <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;evTurnOn();
<a name="l00038"></a>00038         <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;evTurnOff();
<a name="l00039"></a>00039         <a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>-&gt;evCookingDone();
<a name="l00040"></a>00040         <a class="code" href="class_microwave.html#ab307e9aac752df52f87d36e0b92948c9">m_eventsCountUser</a> += 3;
<a name="l00041"></a>00041     }
<a name="l00042"></a>00042 }
<a name="l00043"></a>00043 
<a name="l00044"></a><a class="code" href="class_microwave.html#a5196ac2623e7065ba474b2a7630e4b5b">00044</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a5196ac2623e7065ba474b2a7630e4b5b">Microwave::workersStart</a>()
<a name="l00045"></a>00045 {
<a name="l00046"></a>00046     <a class="code" href="class_microwave.html#a1ec277e6d13d0a963814a13ccc6fa350">m_threadDoor</a> = boost::shared_ptr&lt;boost::thread&gt;(<span class="keyword">new</span> boost::thread(boost::bind(&amp;<a class="code" href="class_microwave.html#a9de439ae4e3768a815cd8653697123dc">Microwave::doorWorker</a>, <span class="keyword">this</span>)));
<a name="l00047"></a>00047     <a class="code" href="class_microwave.html#aba6011d88a4d6311971a534726facc06">m_threadUser</a> = boost::shared_ptr&lt;boost::thread&gt;(<span class="keyword">new</span> boost::thread(boost::bind(&amp;<a class="code" href="class_microwave.html#ad4dbfdafd37a85475b02e5e1a50c6b22">Microwave::userWorker</a>, <span class="keyword">this</span>)));
<a name="l00048"></a>00048 }
<a name="l00049"></a>00049 
<a name="l00050"></a><a class="code" href="class_microwave.html#ad3fbfc146298a3e6f8a9c37603b8da8e">00050</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#ad3fbfc146298a3e6f8a9c37603b8da8e">Microwave::workersStop</a>()
<a name="l00051"></a>00051 {
<a name="l00052"></a>00052     <a class="code" href="class_microwave.html#aab9533a3e37add11113ab8333a99e17a">m_bRun</a> = <span class="keyword">false</span>;
<a name="l00053"></a>00053     <a class="code" href="class_microwave.html#aba6011d88a4d6311971a534726facc06">m_threadUser</a>-&gt;join();
<a name="l00054"></a>00054     <a class="code" href="class_microwave.html#a1ec277e6d13d0a963814a13ccc6fa350">m_threadDoor</a>-&gt;join();
<a name="l00055"></a>00055     BOOST_LOG_L3(<span class="stringliteral">&quot;Door &quot;</span> &lt;&lt; <a class="code" href="class_microwave.html#ac15ca147a5c1c50b84bfc381d49e30b8">m_eventsCountDoor</a>);
<a name="l00056"></a>00056     BOOST_LOG_L3(<span class="stringliteral">&quot;User &quot;</span> &lt;&lt; <a class="code" href="class_microwave.html#ab307e9aac752df52f87d36e0b92948c9">m_eventsCountUser</a>);
<a name="l00057"></a>00057     BOOST_LOG_L3(<span class="stringliteral">&quot;Total &quot;</span> &lt;&lt;  <a class="code" href="class_microwave.html#ac15ca147a5c1c50b84bfc381d49e30b8">m_eventsCountDoor</a> + <a class="code" href="class_microwave.html#ab307e9aac752df52f87d36e0b92948c9">m_eventsCountUser</a> );
<a name="l00058"></a>00058 }
<a name="l00059"></a>00059 
<a name="l00060"></a><a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">00060</a> MicrowaveContext&amp; <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">Microwave::GetContext</a>()
<a name="l00061"></a>00061 {
<a name="l00062"></a>00062     <span class="keywordflow">if</span>(<a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a> != NULL){
<a name="l00063"></a>00063         <span class="keywordflow">return</span> *<a class="code" href="class_microwave.html#ade8a3fc751fde19161408844833bda23">m_pContext</a>;
<a name="l00064"></a>00064     } <span class="keywordflow">else</span> {
<a name="l00065"></a>00065         <span class="keywordflow">throw</span> std::runtime_error(<span class="stringliteral">&quot;context is not set&quot;</span>);
<a name="l00066"></a>00066     }
<a name="l00067"></a>00067 }
<a name="l00068"></a>00068 
<a name="l00069"></a><a class="code" href="class_microwave.html#aa7f4bfe185c0e1feb25048d516bb716f">00069</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#aa7f4bfe185c0e1feb25048d516bb716f">Microwave::evStart</a>()
<a name="l00070"></a>00070 {
<a name="l00071"></a>00071     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evStart();
<a name="l00072"></a>00072 }
<a name="l00073"></a>00073 
<a name="l00074"></a><a class="code" href="class_microwave.html#ad4735ddccdc7096b7466b4dcbe34a891">00074</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#ad4735ddccdc7096b7466b4dcbe34a891">Microwave::evStop</a>()
<a name="l00075"></a>00075 {
<a name="l00076"></a>00076     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evStop();
<a name="l00077"></a>00077 }
<a name="l00078"></a>00078 
<a name="l00079"></a><a class="code" href="class_microwave.html#ac43ce0105d42e3fbf34b836dbc19edf0">00079</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#ac43ce0105d42e3fbf34b836dbc19edf0">Microwave::evTurnOn</a>()
<a name="l00080"></a>00080 {
<a name="l00081"></a>00081     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evTurnOn();
<a name="l00082"></a>00082 }
<a name="l00083"></a>00083 
<a name="l00084"></a><a class="code" href="class_microwave.html#a5614bebde7a67e7d7ec85a9bd168aa5b">00084</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a5614bebde7a67e7d7ec85a9bd168aa5b">Microwave::evCookingDone</a>()
<a name="l00085"></a>00085 {
<a name="l00086"></a>00086     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evCookingDone();
<a name="l00087"></a>00087 }
<a name="l00088"></a>00088 
<a name="l00089"></a><a class="code" href="class_microwave.html#a1edc22f3f85604af9657e3bd2b7ec0db">00089</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a1edc22f3f85604af9657e3bd2b7ec0db">Microwave::evDoorOpened</a>()
<a name="l00090"></a>00090 {
<a name="l00091"></a>00091     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evDoorOpened();
<a name="l00092"></a>00092 }
<a name="l00093"></a>00093 
<a name="l00094"></a><a class="code" href="class_microwave.html#a789a8365e1e2d82b7bb504da761bf933">00094</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a789a8365e1e2d82b7bb504da761bf933">Microwave::evDoorClosed</a>()
<a name="l00095"></a>00095 {
<a name="l00096"></a>00096     <a class="code" href="class_microwave.html#a51c09e2537ff953d6c410c186380bafd">GetContext</a>().evDoorClosed();
<a name="l00097"></a>00097 }
<a name="l00098"></a>00098 
<a name="l00099"></a><a class="code" href="class_microwave.html#a4a54f1b5c66af1f0cf09ca4ff23ad007">00099</a> <span class="keywordtype">void</span> <a class="code" href="class_microwave.html#a4a54f1b5c66af1f0cf09ca4ff23ad007">Microwave::Print</a>(<span class="keyword">const</span> <span class="keywordtype">string</span>&amp; message)
<a name="l00100"></a>00100 {
<a name="l00101"></a>00101     cout &lt;&lt; message &lt;&lt; endl;
<a name="l00102"></a>00102 }
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
